<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="产线或产线组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <a-input v-model="model.line" placeholder="请输入产线或产线组"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品/产品组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partno">
              <a-input v-model="model.partno" placeholder="请输入产品/产品组"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stn">
              <a-input v-model="model.stn" placeholder="请输入工站"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itempartno">
              <a-input v-model="model.itempartno" placeholder="请输入物料"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemrev">
              <a-input v-model="model.itemrev" placeholder="请输入物料版本"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料起始数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtyStart">
              <a-input-number v-model="model.qtyStart" placeholder="请输入物料起始数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料批次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot">
              <a-input v-model="model.lot" placeholder="请输入物料批次"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料使用数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qty">
              <a-input-number v-model="model.qty" placeholder="请输入物料使用数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否共享物料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shared">
              <j-dict-select-tag type="list" v-model="model.shared" dictCode="isEnabled" placeholder="请选择是否共享物料" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="供应商批次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierLot">
              <a-input v-model="model.supplierLot" placeholder="请输入供应商批次"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="上料时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firsttime">
              <j-date placeholder="请选择上料时间"  v-model="model.firsttime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag type="list" v-model="model.status" dictCode="MesMaterialLot_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ctime">
              <j-date placeholder="请选择更新时间"  v-model="model.ctime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MesMaterialLotForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        isEdit:false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           line: [
              { required: true, message: '请输入产线或产线组!'},
           ],
           partno: [
              { required: true, message: '请输入产品/产品组!'},
           ],
           stn: [
              { required: true, message: '请输入工站!'},
           ],
           itempartno: [
              { required: true, message: '请输入物料!'},
           ],
           lot: [
              { required: true, message: '请输入物料批次!'},
           ],
           qty: [
              { required: true, message: '请输入物料使用数量!'},
           ],
           status: [
              { required: true, message: '请输入状态!'},
           ],
        },
        url: {
          add: "/trace/mesMaterialLot/add",
          edit: "/trace/mesMaterialLot/edit",
          queryById: "/trace/mesMaterialLot/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.isEdit = false;
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        if (JSON.stringify(record) !== "{}"){
                 this.isEdit = true
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.isEdit){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>